package com.effectiveui.away3d.primitives{
	import away3d.containers.ObjectContainer3D;
	import away3d.primitives.Plane;
	
	import mx.events.FlexEvent;
	
	public class Plane extends FlexObject3D{
		
		private var _plane:away3d.primitives.Plane = new away3d.primitives.Plane({width:oldWidth, height:oldHeight});
		private var _segmentsW:Number;
		private var _segmentsH:Number;
		private var oldWidth:Number = 1;
		private var oldHeight:Number = 1;
		
		public function Plane(){
			this.object3D = this._plane;
			this.addEventListener(FlexEvent.CREATION_COMPLETE, function():void{invalidateDisplayList()});
		}
		
		public function get segmentsW():Number{
			return this._segmentsW;
		}
		
		public function set segmentsW(value:Number):void{
			this._segmentsW = value;
			this.needUpdate = true;
		}
		
		public function get segmentsH():Number{
			return this._segmentsH;
		}
		
		public function set segmentsH(value:Number):void{
			this._segmentsH = value;
			this.needUpdate = true;
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if(this._plane != null){
				
				if(this.needUpdate || this.numChildren > 0){
					var oParent:ObjectContainer3D = this._plane.parent;
					if(oParent != null){
						oParent.removeChild(this._plane);
						
						this._plane = new away3d.primitives.Plane({material:this.material, width:this.oldWidth, height:this.oldHeight});	
						
						this.needUpdate = false;
						oParent.addChild(this._plane);
					}
				}
				
				var scaleX:Number = this.width/this.oldWidth;
				this._plane.scaleX(scaleX);
			
				var scaleZ:Number = this.height/this.oldHeight;
				this._plane.scaleZ(scaleZ);
				
				this._plane.x = this.x;
				this._plane.y = this.y;
				this._plane.z = this.z; 
				this._plane.rotationX = this.rotationX;
				this._plane.rotationY = this.rotationY;
				this._plane.rotationZ = this.rotationZ;
				this._plane.bothsides = this.bothsides;
			}
			
			this.oldWidth = this.width;
			this.oldHeight = this.height;
			this.includeInLayout = false;
			this.visible = false;
		}
	}
}